﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrmMain
{
    class Dao
    {
        private const string constr = "Data Source=.;Initial Catalog=Food;Integrated Security=True";
        private SqlConnection GetCon()
        {
            return new SqlConnection(constr);
        }
        private SqlConnection GetOpenCon()
        {
            SqlConnection con = GetCon();
            con.Open();
            return con;
        }

        public int DoNonquery(string sql)
        {
            using (SqlConnection con = GetOpenCon())
            {
                SqlCommand com = new SqlCommand(sql, con);
                return com.ExecuteNonQuery();
            }
        }
        public object DoScalar(string sql)
        {
            using (SqlConnection con = GetOpenCon())
            {
                SqlCommand com = new SqlCommand(sql, con);
                return com.ExecuteScalar();
            }
        }
        public DataTable GetTable(string sql)
        {
            SqlDataAdapter da = new SqlDataAdapter(sql, GetCon());
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
